package com.joseph.websocket.test;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        String num = "";
        File file = new File("D:/robot_ + " + num + " +.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        String s;
        List<RobotLog> list = new ArrayList<>();
        TreeMap<Long, Object> map = new TreeMap<>();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        HttpPost httpPost = new HttpPost("http://172.16.0.51:9200/t_im_robot_message/_search");
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        String query = "{\"query\":{\"term\":{\"msgId\":{\"value\":\"%s\"}}}}";

//        while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
//            RobotLog robotLog = JSONObject.parseObject(s, RobotLog.class);
//            RobotLog robotLog1 = JSONObject.parseObject(robotLog.get_source(), RobotLog.class);
//            long msgId = Long.parseLong(robotLog1.getUserMsgId().substring(0, 13));
//            map.put(msgId, robotLog);
//        }
//        for (Map.Entry entry : map.entrySet()) {
//            System.out.println(entry);

        File out = new File("D:/out_ " + num + " .csv");
        FileWriter writer = new FileWriter(out);

        while ((s = br.readLine()) != null) {
            //使用readLine方法，一次读一行
            RobotLog robotLog = JSONObject.parseObject(s, RobotLog.class);
            RobotLog innerLog = JSONObject.parseObject(robotLog.get_source(), RobotLog.class);
            String param = String.format(query, innerLog.getUserMsgId());
            StringEntity qs = new StringEntity(param);
            httpPost.setEntity(qs);
            String a = EntityUtils.toString(httpClient.execute(httpPost).getEntity());
            String b = "";
            if (innerLog == null) {
                continue;
            }
            JSONObject jb = JSONObject.parseObject(innerLog.get_source());
            try {
                b = a.substring(a.indexOf("content") + 10, a.indexOf("empId") - 3);
            } catch (Exception e) {
                continue;
            }
            String line = innerLog.getKnowledgeId() + "," + innerLog.getUserMsgId() + "," + innerLog.getRobotSessionId() + ","
                    + innerLog.getQuestion() + "," + innerLog.getScore() + "," + pre(innerLog.getParticipleResult()) + "," + b;
            System.out.println(line);
            writer.write(line + "\r\n");
        }
    }

    private static String pre(String s) {
        if (s == null) {
            return "";
        }
        return s.replace("\"", "'")
                .replace(",", ":");
    }

}


class RobotLog {
    private String userMsgId;
    private int knowledgeId;
    private String participleResult;
    private String question;
    private double score;
    private String robotSessionId;
    private String origin;
    private String _source;
    private RobotLog robotLog;

    public String getUserMsgId() {
        return userMsgId;
    }

    public void setUserMsgId(String userMsgId) {
        this.userMsgId = userMsgId;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }

    public String get_source() {
        return _source;
    }

    public void set_source(String _source) {
        this._source = _source;
    }

    public int getKnowledgeId() {
        return knowledgeId;
    }

    public void setKnowledgeId(int knowledgeId) {
        this.knowledgeId = knowledgeId;
    }

    public String getParticipleResult() {
        return participleResult;
    }

    public void setParticipleResult(String participleResult) {
        this.participleResult = participleResult;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getRobotSessionId() {
        return robotSessionId;
    }

    public void setRobotSessionId(String robotSessionId) {
        this.robotSessionId = robotSessionId;
    }

    public RobotLog getRobotLog() {
        return robotLog;
    }

    public void setRobotLog(RobotLog robotLog) {
        this.robotLog = robotLog;
    }

    @Override
    public String toString() {
        return "RobotLog{" +
                "userMsgId='" + userMsgId + '\'' +
                ", knowledgeId=" + knowledgeId +
                ", participleResult='" + participleResult + '\'' +
                ", question='" + question + '\'' +
                ", score=" + score +
                ", robotSessionId='" + robotSessionId + '\'' +
                ", origin='" + origin + '\'' +
                ", _source='" + _source + '\'' +
                ", robotLog=" + robotLog +
                '}';
    }
}
